package com.zyy.Household_expenditure.controller;


import com.zyy.Household_expenditure.common.result.R;
import com.zyy.Household_expenditure.entity.HomeBill;
import com.zyy.Household_expenditure.entity.vo.CountVo;
import com.zyy.Household_expenditure.service.HomeBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/bill")
@CrossOrigin
@Api(description = "账单操作接口")

public class HomeBillController {
    @Autowired
    private HomeBillService billService;


//=============================账单的增删改查=============================

    /**
     * 添加账单
     */
    @PostMapping("/addBill")
    @ApiOperation(" 添加账单")
    public R addBill(@RequestBody HomeBill bill) {
        boolean addBill = billService.save(bill);
        if (addBill) {
            return R.ok().message("添加完成");
        } else
            return R.error().message("失败，请尝试重新传");

    }

    /**
     * 账单展示
     */
    @GetMapping("getBill")
    @ApiOperation(" 账单展示")
    public R getBill() {
        List<HomeBill> billData = billService.list(null);
//        isEmpty仅仅是判断空和长度为0字符串
//        isBlank判断的是空,长度为0,空白字符(包括空格,制表符\t,换行符\n,换页符\f,回车\r)组成的字符串。”
        if (!billData.isEmpty()) {
            return R.ok().data("billData", billData);
        } else return R.error().message("无记录");
    }

    /**
     * 个人账单展示
     */
    @GetMapping("/getBillByUid")
    @ApiOperation(" 账单展示")
    public R getBillByUid(HttpServletRequest request) {
        List<HomeBill> billData = billService.getBillByUid(request);
//        isEmpty仅仅是判断空和长度为0字符串
//        isBlank判断的是空,长度为0,空白字符(包括空格,制表符\t,换行符\n,换页符\f,回车\r)组成的字符串。”
        if (!billData.isEmpty()) {
            return R.ok().data("billData", billData);
        } else return R.error().message("无记录");
    }

    /**
     * 分页个人账单查条件询展示
     */
    @PostMapping("/getBillByUid/{current}/{limit}")
    @ApiOperation(" 账单展示")
    public R fyGetBillByUid(HttpServletRequest request, @PathVariable long current, @PathVariable long limit, @RequestBody HomeBill homeBill) {
        Map billData = billService.fyGetBillByUid(request, current, limit, homeBill);
//        isEmpty仅仅是判断空和长度为0字符串
//        isBlank判断的是空,长度为0,空白字符(包括空格,制表符\t,换行符\n,换页符\f,回车\r)组成的字符串。”
        if (!billData.isEmpty()) {
            return R.ok().data("billData", billData);
        } else return R.error().message("无记录");
    }

    /**
     * 分页个人账单查询展示
     */
    @PostMapping("/fyByUid/{current}/{limit}")
    @ApiOperation(" 账单展示")
    public R fyByUid(HttpServletRequest request, @PathVariable long current, @PathVariable long limit) {
        Map billData = billService.fyByUid(request, current, limit);
//        isEmpty仅仅是判断空和长度为0字符串
//        isBlank判断的是空,长度为0,空白字符(包括空格,制表符\t,换行符\n,换页符\f,回车\r)组成的字符串。”
        if (!billData.isEmpty()) {
            return R.ok().data("billData", billData);
        } else return R.error().message("无记录");
    }


    /**
     * 查看消费账单详情
     */
    @GetMapping("/getDetailBill/{id}")
    @ApiOperation("详细账单展示")
    public R getDetailBill(@PathVariable String id) {
        HomeBill detailBill = billService.getById(id);
        if (detailBill != null) {
            return R.ok().data("detailBill", detailBill);

        } else return R.error().message("无记录");

    }

    /**
     * 消费账单删除
     */
    @DeleteMapping("delBill/{id}")
    @ApiOperation(" 账单删除")
    public R delBill(@PathVariable String id) {
        boolean delBill = billService.removeById(id);
        if (delBill) {
            return R.ok().message("删除完成");
        } else
            return R.error().message("失败，请尝试重新删除");
    }

    /**
     * 消费账单修改
     */
    @PostMapping("updateBill")
    @ApiOperation(" 账单修改")

    public R updateBill(@RequestBody HomeBill billEntity) {
        boolean updateBill = billService.updateById(billEntity);
        if (updateBill) {
            return R.ok().message("修改完成");
        } else
            return R.error().message("失败，请尝试重新修改");
    }
//=============================以周为单位的操作=============================

    @GetMapping("/getMenBillTypeWeek/{month}/{week}")
    @ApiOperation("按周为单位，统计某个家庭成员某种消费类型的消费总额")
    public R getMenBillTypeWeek(HttpServletRequest request,  @PathVariable String month,@PathVariable String week) {
        List<CountVo> list = billService.getMenBillTypeWeek(request, week,month);
        return R.ok().data("list",list);
    }
//=============================以月为单位的操作=============================

    /**
     * 统计某个家庭成员某种消费类型的消费总额
     */
    @GetMapping("/getMenBillTypeMonth/{month}")
    @ApiOperation("按月为单位，统计某个家庭成员某种消费类型的消费总额")
    public R getMenBillTypeMonth(HttpServletRequest request, @PathVariable String month) {
        List<CountVo> list = billService.getMenBillTypeMonth(request, month);
        return R.ok().data("list",list);
    }



}

